/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nc.kyiv.yellow.sociallibrary.bookinfo.web;

import com.nc.kyiv.yellow.sociallibrary.beans.bookinfo.BookInfoBeanLocal;
import com.nc.kyiv.yellow.sociallibrary.beans.bookinfo.BookInfoBeanLocalHome;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.CreateException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.text.html.HTMLDocument;

/**
 *
 * @author My
 */
public class AddBookServlet extends HttpServlet {

    
        private BookInfoBeanLocal bookInfoBean = null;

    @Override
    public void init() throws ServletException {
        super.init();
        bookInfoBean = lookupBookInfoBeanLocal();
    }
            
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {

            Map params = new HashMap<String, String>();
            params.put("isbn", request.getParameter("isbn"));
            params.put("title", request.getParameter("title"));
            params.put("description", request.getParameter("description"));
            params.put("page_number", request.getParameter("page_number"));
                                    

            /* TODO output your page here. You may use following sample code. */
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet AddBookServlet</title>");            
            out.println("</head>");
            out.println("<body>");
            bookInfoBean.addBookInfo(params);
            out.println("Book was added");
            out.println("</body>");
            out.println("</html>");
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private BookInfoBeanLocal lookupBookInfoBeanLocal() {
        try {
            Context c = new InitialContext();
            BookInfoBeanLocalHome rv = (BookInfoBeanLocalHome) c.lookup("java:comp/env/BookInfoBean");
            return rv.create();
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        } catch (CreateException ce) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ce);
            throw new RuntimeException(ce);
        }
    }
    
}
